package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.exception.PlatformBizErrorCodes;
import com.iwomy.secureplat.common.core.util.MsgUtils;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.platform.dto.BasicAlarmContactsDTO;
import com.iwomy.secureplat.platform.entity.BasicAlarmContactsEntity;
import com.iwomy.secureplat.platform.entity.BasicAlarmContactsTimeEntity;
import com.iwomy.secureplat.platform.entity.BasicInstallLocationEntity;
import com.iwomy.secureplat.platform.service.BasicAlarmContactsService;
import com.iwomy.secureplat.platform.service.BasicAlarmContactsTimeService;
import com.iwomy.secureplat.platform.service.BasicInstallLocationService;
import com.iwomy.secureplat.platform.vo.BasicAlarmContactsVO;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接警联系人
 *
 * @author pig
 * @date 2023-12-01 11:54:13
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicAlarmContacts" )
@Tag(description = "basicAlarmContacts" , name = "接警联系人管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicAlarmContactsController {

    private final  BasicAlarmContactsService basicAlarmContactsService;

    private final BasicInstallLocationService basicInstallLocationService;

	private final BasicAlarmContactsTimeService basicAlarmContactsTimeService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param basicAlarmContactsDTO 接警联系人
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('platform_basicAlarmContacts_view')" )
    public R getBasicAlarmContactsPage(@ParameterObject Page page, @ParameterObject BasicAlarmContactsDTO basicAlarmContactsDTO) {
        return R.ok(basicAlarmContactsService.getBasicAlarmContactsPage(page, basicAlarmContactsDTO));
    }

	/**
	 * 根据部门id查询对应接警人
	 * @param basicAlarmContactsEntity 接警联系人
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/list")
	public R getList( @ParameterObject BasicAlarmContactsEntity basicAlarmContactsEntity) {
		return R.ok(basicAlarmContactsService.getBasicAlarmContactsList(basicAlarmContactsEntity));
	}

    /**
     * 通过id查询接警联系人
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('platform_basicAlarmContacts_view')" )
    public R getById(@PathVariable("id" ) Long id) {
		BasicAlarmContactsVO basicAlarmContactsVO =	basicAlarmContactsService.getVoById(id);
		if (basicAlarmContactsVO.getAlarmTime() != null && basicAlarmContactsVO.getAlarmTime() == 20) {
			LambdaQueryWrapper<BasicAlarmContactsTimeEntity> wrapper = Wrappers.lambdaQuery();
			wrapper.eq(BasicAlarmContactsTimeEntity::getAlarmContactsId,id);
			List<BasicAlarmContactsTimeEntity> alarmContactsTimeList = basicAlarmContactsTimeService.list(wrapper);
			basicAlarmContactsVO.setAlarmContactsTimeList(alarmContactsTimeList);
		}
        return R.ok(basicAlarmContactsVO);
    }

	/**
	 * 通过接警联系人id查询绑定安装点列表
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询" , description = "通过id查询" )
	@GetMapping("/getAlarmInstallList/{id}" )
	public R getAlarmInstallList(@ParameterObject Page page,@PathVariable("id" ) Long id) {
		return R.ok(basicInstallLocationService.getAlarmInstallList(page,id));
	}

    /**
     * 新增接警联系人
     * @param basicAlarmContacts 接警联系人
     * @return R
     */
    @Operation(summary = "新增接警联系人" , description = "新增接警联系人" )
    @SysLog("新增接警联系人" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform_basicAlarmContacts_add')" )
    public R save(@RequestBody BasicAlarmContactsEntity basicAlarmContacts) {
		BasicAlarmContactsEntity wrapperEntity = new BasicAlarmContactsEntity();
		wrapperEntity.setPhone(basicAlarmContacts.getPhone());
		LambdaQueryWrapper<BasicAlarmContactsEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		wrapper.eq(BasicAlarmContactsEntity::getDeptId, basicAlarmContacts.getDeptId());
		BasicAlarmContactsEntity one = basicAlarmContactsService.getOne(wrapper);
		if(ObjectUtil.isNotNull(one)){
//			throw new PlatformException("电话号码已存在");
		}
		basicAlarmContacts.setAlarmTime(10);
        return R.ok(basicAlarmContactsService.save(basicAlarmContacts));
    }

    /**
     * 修改接警联系人
     * @param basicAlarmContacts 接警联系人
     * @return R
     */
    @Operation(summary = "修改接警联系人" , description = "修改接警联系人" )
    @SysLog("修改接警联系人" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform_basicAlarmContacts_edit')" )
    public R updateById(@RequestBody BasicAlarmContactsEntity basicAlarmContacts) {
		BasicAlarmContactsEntity wrapperEntity = new BasicAlarmContactsEntity();
		wrapperEntity.setPhone(basicAlarmContacts.getPhone());
		LambdaQueryWrapper<BasicAlarmContactsEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		wrapper.eq(BasicAlarmContactsEntity::getDeptId, basicAlarmContacts.getDeptId());
		BasicAlarmContactsEntity one = basicAlarmContactsService.getOne(wrapper);
		if(ObjectUtil.isNotNull(one)&&!one.getId().equals(basicAlarmContacts.getId())){
//			throw new PlatformException("电话号码已存在");
		}
        return R.ok(basicAlarmContactsService.updateById(basicAlarmContacts));
    }

    /**
     * 通过id删除接警联系人
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除接警联系人" , description = "通过id删除接警联系人" )
    @SysLog("通过id删除接警联系人" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform_basicAlarmContacts_del')" )
    public R removeById(@RequestBody Long[] ids) {
		long count = basicInstallLocationService.count(Wrappers.<BasicInstallLocationEntity>lambdaQuery()
				.in(BasicInstallLocationEntity::getAlarmWorkerId, ids));
		if (count > 0) {
//			return R.failed("已绑定安装点无法删除，请先解除绑定");
			return R.failed(MsgUtils.getMessage(PlatformBizErrorCodes.PLACEBUILDING_USED_DELETE));
		}
        return R.ok(basicAlarmContactsService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param basicAlarmContacts 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('platform_basicAlarmContacts_export')" )
    public List<BasicAlarmContactsEntity> export(BasicAlarmContactsEntity basicAlarmContacts,Long[] ids) {
        return basicAlarmContactsService.list(Wrappers.lambdaQuery(basicAlarmContacts).in(ArrayUtil.isNotEmpty(ids), BasicAlarmContactsEntity::getId, ids));
    }
}